package com.zygh.lz.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.dao.DutyModelMapper;
import com.zygh.lz.dao.DutyPostMapper;
import com.zygh.lz.dao.DutyPostModelMapper;
import com.zygh.lz.entity.duty.DutyModel;
import com.zygh.lz.entity.duty.DutyPost;
import com.zygh.lz.entity.duty.DutyPostModel;
import com.zygh.lz.service.DutyService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lee
 */
@Service
public class DutyServiceImpl implements DutyService {
    @Resource
    private DutyModelMapper dutyModelMapper;
    @Resource
    private DutyPostMapper dutyPostMapper;
    @Resource
    private DutyPostModelMapper dutyPostModelMapper;

    @Override
    public ResultBean model(DutyModel dutyModel) {
        PageHelper.startPage(dutyModel.getPageNum(), dutyModel.getPageSize());
        List<DutyModel> dutyModels = dutyModelMapper.selectList(Wrappers.<DutyModel>lambdaQuery()
        .like(Objects.nonNull(dutyModel.getName()),DutyModel::getName,dutyModel.getName())
        .orderByAsc(DutyModel::getId));
        PageInfo<DutyModel> info = new PageInfo<>(dutyModels);
        return ResultUtil.setOK("查询成功",info);
    }

    @Override
    public ResultBean post(DutyPost dutyPost) {
        PageHelper.startPage(dutyPost.getPageNum(),dutyPost.getPageSize());
        List<DutyPost> dutyPosts = dutyPostMapper.selectList(Wrappers.<DutyPost>lambdaQuery()
                .like(Objects.nonNull(dutyPost.getPostName()),DutyPost::getPostName,dutyPost.getPostName())
        .orderByDesc(DutyPost::getId));
        PageInfo<DutyPost> info = new PageInfo<>(dutyPosts);
        return ResultUtil.setOK("查询成功",info);
    }

    @Override
    public ResultBean postAdd(DutyPost dutyPost) {

        List<DutyPost> dutyPosts = dutyPostMapper.selectList(Wrappers.emptyWrapper());
        Integer[] integers = dutyPosts.stream().map(DutyPost::getPostCode).toArray(Integer[]::new);
        int randomExcept = getRandomExcept(100, integers);
        dutyPost.setLastTime(DateUtil.now());
        dutyPost.setPostCode(randomExcept);
        int insert = dutyPostMapper.insert(dutyPost);
        for (Integer integer : dutyPost.getModeCodes()) {
            DutyPostModel dutyPostModel = new DutyPostModel();
            dutyPostModel.setPostId(dutyPost.getPostCode());
            dutyPostModel.setModeId(integer);
            dutyPostModelMapper.insert(dutyPostModel);
        }

        return ResultUtil.execOp(insert,"插入");
    }

    @Override
    public ResultBean modelAdd(DutyModel dutyModel) {
        List<DutyModel> dutyPosts = dutyModelMapper.selectList(Wrappers.emptyWrapper());
        Integer[] integers = dutyPosts.stream().map(DutyModel::getCode).toArray(Integer[]::new);
        int randomExcept = getRandomExcept(100, integers);
        dutyModel.setCode(randomExcept);
        dutyModel.setLastTime(DateUtil.now());
        Integer integer = dutyModelMapper.insertId(dutyModel);

        return ResultUtil.execOp(integer,"插入");
    }

    @Override
    public ResultBean postUpdate(DutyPost dutyPost) {
        int update = dutyPostMapper.update(dutyPost, Wrappers.<DutyPost>lambdaQuery()
                .eq(DutyPost::getId, dutyPost.getId()));
        dutyPostModelMapper.delete(Wrappers.<DutyPostModel>lambdaQuery().eq(DutyPostModel::getPostId,dutyPost.getPostCode()));

        List<Integer> modeCodes = dutyPost.getModeCodes();
        for (Integer modeCode:modeCodes){
            DutyPostModel dutyPostModel = new DutyPostModel();
            dutyPostModel.setPostId(dutyPost.getPostCode());
            dutyPostModel.setModeId(modeCode);
            dutyPostModelMapper.insert(dutyPostModel);
        }
        return ResultUtil.execOp(update,"修改");
    }

    @Override
    public ResultBean modelUpdate(DutyModel dutyModel) {
        int update = dutyModelMapper.update(dutyModel, Wrappers.<DutyModel>lambdaQuery()
                .eq(DutyModel::getId, dutyModel.getId()));
        return ResultUtil.execOp(update,"修改");
    }

    @Override
    public ResultBean postDelete(DutyPost dutyPost) {
        int i = dutyPostMapper.deleteById(dutyPost.getId());
        return ResultUtil.execOp(i,"删除");
    }

    @Override
    public ResultBean modelDelete(DutyModel dutyModel) {
        int i = dutyModelMapper.deleteById(dutyModel.getId());
        return ResultUtil.execOp(i,"删除");
    }

    @Override
    public ResultBean modelByPostCode(Integer postCode) {
        List<DutyPostModel> dutyPostModels = dutyPostModelMapper.selectList(Wrappers.<DutyPostModel>lambdaQuery()
                .eq(DutyPostModel::getPostId, postCode));
        List<Integer> collect = dutyPostModels.stream().map(DutyPostModel::getModeId).collect(Collectors.toList());

        List<DutyModel> data = dutyModelMapper.selectList(Wrappers.<DutyModel>lambdaQuery().in(DutyModel::getCode, collect));
        return ResultUtil.setOK("查询成功",data);
    }

    @Override
    public ResultBean postAll(DutyPost dutyPost) {
        List<DutyPost> dutyPosts = dutyPostMapper.selectList(Wrappers.emptyWrapper());
        List<Integer> collect = dutyPosts.stream().map(DutyPost::getPostCode).collect(Collectors.toList());

        List<DutyPostModel> dutyPostModels = dutyPostModelMapper.selectList(Wrappers.<DutyPostModel>lambdaQuery().
                in(DutyPostModel::getPostId, collect));
        List<Integer> collect1 = dutyPostModels.stream().map(DutyPostModel::getModeId).collect(Collectors.toList());

        List<DutyModel> dutyModels = dutyModelMapper.selectList(Wrappers.<DutyModel>lambdaQuery().
                in(DutyModel::getCode, collect1));

        for (DutyPost post : dutyPosts) {
            List<Integer> modelCodes = new ArrayList<>();
            for(DutyPostModel dutyPostModel:dutyPostModels){
                if(Objects.equals(post.getPostCode(),dutyPostModel.getPostId())){
                    modelCodes.add(dutyPostModel.getModeId());
                }
            }
            String collect2 = dutyModels.stream().filter(x -> modelCodes.contains(x.getCode())).map(DutyModel::getName).collect(Collectors.joining(","));

            post.setPostMode(collect2);
        }

        return ResultUtil.setOK("查询成功",dutyPosts);
    }

    @Override
    public ResultBean postId(DutyPost dutyPost) {
      DutyPost data= dutyPostMapper.selectOne(Wrappers.<DutyPost>lambdaQuery()
        .eq(DutyPost::getId,dutyPost.getId()));
        return ResultUtil.setOK("查询成功",data);
    }

    @Override
    public ResultBean findDutyModeAll() {
        return ResultUtil.setOK("查询成功",dutyModelMapper.findDutyModeAll());
    }



    /**
     * @param randMax    最大值
     * @param exceptNums 不能包含的随机数
     * @return 随机数
     */
    private static int getRandomExcept(int randMax, Integer[] exceptNums) {
        Random rand = new Random();
        int num = rand.nextInt(randMax);
        while (true) {
            int have = 0;
            for (Integer exceptNum : exceptNums) {
                if (num == exceptNum) {
                    have = 1;
                }
            }
            if (have == 0) {
                return num;
            }
            num = rand.nextInt(randMax);
        }
    }
}
